
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _babelHelperVueJsxMergeProps = _interopRequireDefault(require('@vue/babel-helper-vue-jsx-merge-props'));

const _utils = require('../utils');

const _functional = require('../utils/functional');

const _cell = _interopRequireDefault(require('../cell'));

// Utils
// Components
const _createNamespace = (0, _utils.createNamespace)('coupon-cell');
const createComponent = _createNamespace[0];
const bem = _createNamespace[1];
const t = _createNamespace[2];

function formatValue(props) {
  const { coupons } = props;
  const { chosenCoupon } = props;
  const { currency } = props;
  const coupon = coupons[+chosenCoupon];

  if (coupon) {
    const value = coupon.value || coupon.denominations || 0;
    return `-${currency}${(value / 100).toFixed(2)}`;
  }

  return coupons.length === 0 ? t('tips') : t('count', coupons.length);
}

function CouponCell(h, props, slots, ctx) {
  const valueClass = props.coupons[+props.chosenCoupon] ? 'van-coupon-cell--selected' : '';
  const value = formatValue(props);
  return h(_cell.default, (0, _babelHelperVueJsxMergeProps.default)([{
    class: bem(),
    attrs: {
      value,
      title: props.title || t('title'),
      border: props.border,
      isLink: props.editable,
      valueClass,
    },
  }, (0, _functional.inherit)(ctx, true)]));
}

CouponCell.model = {
  prop: 'chosenCoupon',
};
CouponCell.props = {
  title: String,
  coupons: {
    type: Array,
    default: function _default() {
      return [];
    },
  },
  currency: {
    type: String,
    default: '¥',
  },
  border: {
    type: Boolean,
    default: true,
  },
  editable: {
    type: Boolean,
    default: true,
  },
  chosenCoupon: {
    type: [Number, String],
    default: -1,
  },
};

const _default2 = createComponent(CouponCell);

exports.default = _default2;
